package com.lykj.llw.box.ui.diafrg

import android.content.Intent
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.TextPaint
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.text.style.ForegroundColorSpan
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.NonNull
import com.lykj.lib_base.base.BaseDiaFrg
import com.lykj.lib_base.constants.BConstants
import com.lykj.lib_base.internal.extfun.clickWithTrigger
import com.lykj.lib_base.internal.inter.DiaFrgOnClickListener
import com.lykj.llw.box.databinding.DiafrgAuthorizationBinding
import com.lykj.llw.box.ui.activity.CommonWebViewActivity

/**
 * ================================================
 * 隐私授权弹窗
 * ================================================
 *
 * @author gosha
 * @date 2023/7/19-16:57
 */
class AuthorizationDiaFrg : BaseDiaFrg() {
    lateinit var binding: DiafrgAuthorizationBinding

    private var mListener: DiaFrgOnClickListener? = null

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = DiafrgAuthorizationBinding.inflate(layoutInflater)

        initUI()
        initData()
        setListener()

        return binding.root
    }

    override fun onStart() {
        super.onStart()

        val window = dialog!!.window
        //设置窗口弹出动画
        window!!.setWindowAnimations(0)
        window!!.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        val layoutParams = window.attributes
        layoutParams.apply {
            dimAmount = 0.75f
            gravity = Gravity.CENTER
        }

        window.attributes = layoutParams
    }

    open fun setDiaFrgOnClickListener(callback: DiaFrgOnClickListener): AuthorizationDiaFrg {
        mListener = callback
        return this
    }

    override fun initUI() {
        dealWithStrShader()
    }

    /**
     * 设置协议内容《服务协议》《隐私政策》的颜色和点击事件
     */
    private fun dealWithStrShader() {
        val protocol =
            "我们根据相关法律制定了《用户使用协议》和《隐私政策》，请点击同意之前仔细阅读并充分理解相关条款。"
        val builder = SpannableStringBuilder(protocol)
        val cs1: ClickableSpan = object : ClickableSpan() {
            override fun updateDrawState(ds: TextPaint) {
                ds.color = ds.linkColor
                ds.isUnderlineText = false
            }

            override fun onClick(@NonNull widget: View) {
                val intent =
                    Intent(requireContext(), CommonWebViewActivity::class.java)
                intent.putExtra("title", "用户协议")
                intent.putExtra("url", BConstants.USER_PROTOCOL)
                startActivity(intent)
            }
        }
        val cs2: ClickableSpan = object : ClickableSpan() {
            override fun updateDrawState(ds: TextPaint) {
                ds.color = ds.linkColor
                ds.isUnderlineText = false
            }

            override fun onClick(@NonNull widget: View) {
                val intent =
                    Intent(requireContext(), CommonWebViewActivity::class.java)
                intent.putExtra("title", "隐私政策")
                intent.putExtra("url", BConstants.PRIVACY_PROTOCOL)
                startActivity(intent)
            }
        }

        val tc1 = ForegroundColorSpan(Color.parseColor("#FFDD4E03"))
        val tc2 = ForegroundColorSpan(Color.parseColor("#FFDD4E03"))
        builder.setSpan(cs1, 11, 18, Spanned.SPAN_INCLUSIVE_INCLUSIVE)
        builder.setSpan(cs2, 20, 26, Spanned.SPAN_INCLUSIVE_INCLUSIVE)
        builder.setSpan(tc1, 11, 18, Spanned.SPAN_INCLUSIVE_INCLUSIVE)
        builder.setSpan(tc2, 20, 26, Spanned.SPAN_INCLUSIVE_INCLUSIVE)
        binding.tvContent.movementMethod = LinkMovementMethod.getInstance()
        binding.tvContent.highlightColor = resources.getColor(android.R.color.transparent)
        binding.tvContent.text = builder
    }

    override fun initData() {
    }

    override fun setListener() {
        binding.tvBtnCancel.clickWithTrigger {
            dismissAllowingStateLoss()
            mListener?.cancel()
        }
        binding.tvBtnAgree.clickWithTrigger {
            dismissAllowingStateLoss()
            mListener?.sure("")
        }
    }

    override fun onDestroy() {
        super.onDestroy()
    }
}